package org.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CookieUtil {
    public static Map<String, String> parseCookies(List<String> setCookieHeaders) {
        Map<String, String> cookies = new HashMap<>();
        if (setCookieHeaders != null) {
            for (String header : setCookieHeaders) {
                String[] cookieParts = header.split(";");
                String cookieName = null;
                String cookieValue = null;

                for (String part : cookieParts) {
                    part = part.trim();
                    if (cookieName == null && part.contains("=")) {
                        String[] nameValue = part.split("=", 2);
                        cookieName = nameValue[0];
                        cookieValue = nameValue.length > 1 ? nameValue[1] : "";
                        cookies.put(cookieName, cookieValue);
                    }
                }

                if (cookieName != null) {
                    cookies.put(cookieName, cookieValue);
                }
            }
        }
        return cookies;
    }
}
